$(document).ready(function(){
	$('input[type="checkbox"]._checkbox_').live('change', function(){
		var c = $(this).attr('class').replace('_checkbox_', '');
		c = $.trim(c);
		$('input[type="checkbox"].'+ c).attr('checked', $(this).attr('checked'));
	});

	if($.datepicker){
		$('input.date').datepicker({
			dateFormat: 'dd.mm.yy',
			regional: 'ru'
		});
	}

	if($.colorbox){
		$('a.colorbox').colorbox();
	}
	
	if($.tinyMCE){
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			editor_selector : "redactor",

			plugins : "pagebreak,table,save,emotions,media,fullscreen,xhtmlxtras,visualchars,nonbreaking,inlinepopups",

			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,media,|,fullscreen",
			theme_advanced_buttons4 : "cite,abbr,acronym,del,ins,|,visualchars,nonbreaking,template,blockquote,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : false,

			language: 'ru',

			skin : "o2k7",
			skin_variant : ""/*,

			content_css : "css/example.css",

			template_external_list_url : "js/template_list.js",
			external_link_list_url : "js/link_list.js",
			external_image_list_url : "js/image_list.js",
			media_external_list_url : "js/media_list.js"*/
		});
	}
	
	$("a.delete").click(function(){
		a = $(this);
		tr = a.closest('tr');
		
		title = a.attr("title") != "" ? a.attr("title") : "Подтвердите удаление";
		if(!confirm(title)) return false;
		
		$.ajax({
			"url": a.attr('href'),
			"method": "get",
			"dataType": "html",
			"beforeSend": function(){
				a.addClass('load');
			},
			"success": function(html){
				a.removeClass('load');
				if(html == 1) {
					tr.remove();
				} else {
					alert("Удаление не выполнено! Ответ сервера: "+ html);
				}
			},
			"error": function(){
				a.removeClass('load');
				alert("Удаление не выполнено!");
			}
		});
		
		return false;
	});
	
	$(".slide_toggle").slideUp();
	
	$(".slide_handler").live('click', function(){
		var i = $.inArray(this, $(".slide_handler"));
		var toggle = $(".slide_toggle").get(i)
		if(i >=0 ) $(toggle).slideToggle();
		return false;
	});
	
	$("table.ajax_edit a.edit").live("click", function(){
		id = $(this).attr('rel');
		tr_edit = $('table.ajax_edit tr.edit[rel="'+ id +'"]');
		tr_save = $('table.ajax_edit tr.save[rel="'+ id +'"]');
		
		tr_edit.show();
		tr_save.hide();
		
		return false;
	});
	
	$('table.ajax_edit tr.edit input[type="reset"]').live("click", function(){
		id = $(this).attr('rel');
		if(id <= 0) return false;
		tr_edit = $('table.ajax_edit tr.edit[rel="'+ id +'"]');
		tr_save = $('table.ajax_edit tr.save[rel="'+ id +'"]');
		
		tr_edit.hide();
		tr_save.show();
	});
});